﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AIC_CS
{
    public partial class ViewBaoCaoDinhKem : System.Web.UI.Page
    {
        private PrepareDataForTreeView DataForTreeView = new PrepareDataForTreeView();
        private static string FilePath = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDataToDropDownList();
                DataForTreeView.PopulateRootLevel(trvDonVi);
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("Index.aspx");
        }

        protected void BindDataToDropDownList()
        {
            string sql = "SELECT * FROM dbo.tblDanhMuc where MaDM = 'BaoCaoDinhKem'";
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql);
            ddlTenBaoCao.DataSource = dt;
            ddlTenBaoCao.DataTextField = "TenDM";
            ddlTenBaoCao.DataValueField = "ID";
            ddlTenBaoCao.DataBind();
            ddlTenBaoCao.Items.Insert(0, new ListItem("Chọn tên báo cáo", "-1"));
            dt = null;
            sql = "SELECT * FROM dbo.tblDanhMuc WHERE MaDM = 'KyBaoCao'";
            dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql);
            ddlKyBaoCao.DataSource = dt;
            ddlKyBaoCao.DataTextField = "TenDM";
            ddlKyBaoCao.DataValueField = "ID";
            ddlKyBaoCao.DataBind();
            ddlKyBaoCao.Items.Insert(0, new ListItem("Chọn kỳ báo cáo", "-1"));
        }

        protected void ViewBaoCao(int TenBaoCaoID_DM, int KyBaoCao, int DonViID)
        {
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@TenBaoCaoID_DM", SqlDbType.Int, ParameterDirection.Input, TenBaoCaoID_DM));
            arrParams.Add(SqlHelper.CreateParameterObject("@KyBaoCaoID_DM", SqlDbType.Int, ParameterDirection.Input, KyBaoCao));
            arrParams.Add(SqlHelper.CreateParameterObject("@DonVi_ID", SqlDbType.Int, ParameterDirection.Input, DonViID));
            string sql = "SELECT * FROM dbo.tblBaoCaoAttach WHERE BaoCaoAttachID_DM = @TenBaoCaoID_DM AND KyBaoCaoID_DM = @KyBaoCaoID_DM AND DonVi_ID = @DonVi_ID";
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql, arrParams);
            if (dt.Rows.Count == 0)
            {
                BaoCaoDinhKem.InnerText = "Không có báo cáo nào trong kỳ này";
                lbtFileDinhkem.Text = "Không có file đính kèm";
            }
            else
            {
                BaoCaoDinhKem.InnerHtml = dt.Rows[0][5].ToString();
                FilePath = dt.Rows[0][6].ToString();
                if (dt.Rows[0][6].ToString().Trim().Length == 0)
                {
                    lbtFileDinhkem.Text = "Không có file đính kèm";
                }
                else
                {
                    lbtFileDinhkem.Text = dt.Rows[0][6].ToString().Split('/')[2];
                }
            }
        }

        protected void trvDonVi_SelectedNodeChanged(object sender, EventArgs e)
        {

        }

        protected void trvDonVi_TreeNodePopulate(object sender, TreeNodeEventArgs e)
        {
            DataForTreeView.PopulateSubLevel(Int32.Parse(e.Node.Value), e.Node);
        }

        public static void Download(string sFileName, string sFilePath)
        {
            HttpContext.Current.Response.ContentType = "APPLICATION/OCTET-STREAM";
            String Header = "Attachment; Filename=" + sFileName;
            HttpContext.Current.Response.AppendHeader("Content-Disposition", Header);
            System.IO.FileInfo Dfile = new System.IO.FileInfo(HttpContext.Current.Server.MapPath(sFilePath));
            HttpContext.Current.Response.WriteFile(Dfile.FullName);
            HttpContext.Current.Response.End();
        }

        protected void lbtFileDinhkem_Click(object sender, EventArgs e)
        {
            if (FilePath.Trim().Length != 0)
            {
                string FileName = FilePath.Split('/')[2];
                Download(FileName, FilePath);
            }
        }

        protected void btnXemBaoCao_Click(object sender, EventArgs e)
        {
            lblNotice.Visible = false;
            if (trvDonVi.SelectedNode == null)
            {
                lblNotice.Visible = true;
                lblNotice.InnerText = "Vui lòng chọn đơn vị xuất báo cáo";
            }
            else
            {
                FilePath = "";
                BaoCaoDinhKem.Visible = true;
                int ID = Convert.ToInt32(ddlTenBaoCao.SelectedValue);
                int KyBaoCao = Convert.ToInt32(ddlKyBaoCao.SelectedValue);
                int DonVi = Convert.ToInt32(trvDonVi.SelectedNode.Value);
                ViewBaoCao(ID, KyBaoCao, DonVi);
            }
        }
    }
}
